<template>
  <div>
    <bread></bread>
    <div class="InputButton">
      <el-row :gutter="20">
        <el-col :span="5">
          <el-input
            v-model="queryunserInfo.input"
            placeholder="账号"
          ></el-input>
        </el-col>
        <el-col :span="5">
          <el-input
            v-model="queryunserInfo.Input1"
            placeholder="姓名"
          ></el-input>
        </el-col>
        <el-button type="primary" @click="qury">查询</el-button>
        <el-button type="primary" @click="addMember">新增</el-button>
        <el-button @click="resetData">重置</el-button>
      </el-row>
    </div>
    <!-- 数据展示 -->
    <el-table
      height="390px"
      :data="Member"
      border
      style="width: 100%; margin-top: 7px;"
    >
      <el-table-column type="index" label="序号" width="70"> </el-table-column>
      <el-table-column prop="cardNum" label="账号" width="130">
      </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="birthday" label="生日"> </el-table-column>
      <el-table-column prop="phone" label="电话"> </el-table-column>

      <el-table-column prop="money" label="薪酬"> </el-table-column>
      <el-table-column prop="" label="操作" width="160">
        <template v-slot="scope">
          <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="remove(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据展示 -->
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 7, 8, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 分页结束 -->

    <!-- 添加数据对话框 -->
    <el-dialog title="员工编辑" :visible.sync="dialogReFormVisible" width="40%">
      <el-form :model="Meberusername" label-width="80px" :rules="MeberForm">
        <el-form-item label="账号" prop="cardNum">
          <el-input v-model="Meberusername.cardNum"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="Meberusername.name"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="birthday">
          <el-date-picker
            type="date"
            placeholder="入职时间"
            style="width: 100%;"
            v-model="Meberusername.birthday"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="Meberusername.phone"></el-input>
        </el-form-item>
        <el-form-item label="薪酬">
          <el-input v-model="Meberusername.money"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogReFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMester">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加数据对话框结束 -->

    <!-- 编辑数据弹框 -->
    <el-dialog title="员工编辑" :visible.sync="MemberRediong">
      <el-form
        :model="editForm"
        :rules="MeberForm"
        label-width="80px"
        width="40%"
      >
        <el-form-item label="账号" prop="cardNum">
          <el-input v-model="editForm.cardNum"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="入职时间">
          <el-date-picker
            type="date"
            placeholder="入职日期"
            style="width: 100%;"
            v-model="editForm.birthday"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="薪酬">
          <el-input v-model="editForm.money"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="MemberRediong = false">取 消</el-button>
        <el-button type="primary" @click="editRForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import bread from "../components/bread";
export default {
  components: { bread },
  data() {
    return {
      input: "",
      Member: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      dialogReFormVisible: false,
      // 添加数据的信息
      Meberusername: {
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        integral: "",
        money: 0,
        payType: 0,
        address: "",
      },
      Select: "",
      // 添加数据的信息结束
      MeberForm: {
        cardNum: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "姓名不能为空",
            trigger: "blur",
          },
        ],
      },
      MemberRediong: false,
      //编辑数据信息
      editForm: {
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        integral: "",
        money: "",
        payType: "",
        address: "",
      },
      //查询数据
      queryunserInfo: {
        input: "",
        Input1: "",
        Select: "",
        birthday: "",
      },
    };
  },
  created() {
    this.member();
  },
  methods: {
    //获取会员管理的接口
    member() {
      this.axios
        .post("http://mengxuegu.com:9999/pro-api/member/list/search/1/20")
        .then((res) => {
          this.Member = res.data.data.rows;
          this.total = res.data.data.total;
        });
    },
    //获取会员管理的接口
    //分页
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.member();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.member();
    },
    //分页结束
    //添加
    addMember() {
      //打开弹出框
      this.dialogReFormVisible = true;
    },
    //添加数据
    addMester() {
      this.axios
        .post("http://mengxuegu.com:9999/pro-api/member", this.Meberusername)
        .then((res) => {
          if (res.data.code !== 2000) {
            this.$message.error("数据添加失败");
          } else {
            //提示成功
            this.$message.success("数据添加成功");
            //重新加载数据
            this.member();
            this.dialogReFormVisible = false;
          }
        });
    },
    //添加数据结束

    //编辑
    edit(data) {
      console.log(data);
      this.MemberRediong = true;
      //获取原来的数据
      this.editForm = data;
    },
    editRForm() {
      this.axios
        .put("http://mengxuegu.com:9999/pro-api/member/:id", this.editForm)
        .then((res) => {
          console.log(res);
          if (res.data.code !== 2000) {
            this.$message.error("编辑用户失败");
          } else {
            this.$message.success("编辑用户成功");
            this.member();
            this.MemberRediong = false;
          }
        });
    },

    //删除
    remove(id) {
      this.$confirm("此操作将永久删除该员工, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete("http://mengxuegu.com:9999/pro-api/member/:id")
            .then((res) => {
              if (res.data.code !== 2000) {
                this.$message.err("删除失败");
              } else {
                this.$message.success("删除成功");
                this.member();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //查询
    qury() {
      this.axios
        .get("http://mengxuegu.com:9999/pro-api/member/id", this.queryunserInfo)
        .then((res) => {
          if (res.data.code !== 2000) {
            this.$message.error("搜索用户失败");
          } else {
            this.$message.success("查询用户成功");
            //重新加载数据
            this.member();
            this.queryunserInfo.input = "";
            this.queryunserInfo.Input1 = "";
            this.queryunserInfo.Select = "";
            this.queryunserInfo.birthday = "";
          }
        });
    },
    //重置
    resetData() {
      this.queryunserInfo.input = "";
      this.queryunserInfo.Input1 = "";
      this.queryunserInfo.Select = "";
      this.queryunserInfo.birthday = "";
    },
  },
};
</script>

<style scoped="scoped">
.box-card {
  height: 55px;
  padding-left: 20px;
  border-radius: 4px;
}
.el-row {
  width: 100%;
  display: flex;
}
.InputButton {
  display: flex;
  margin-top: 15px;
}
</style>
